package leetcode.medium.limit50;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yaoyao1.liu@zhaopin.com.cn
 * @since 2022-06-30 16:49
 **/
public class No17 {


    public static void main(String[] args) {
        No17 no17 = new No17();
        List<String> list = no17.letterCombinations("234");
        System.out.println(list);
    }

    public List<String> letterCombinations(String digits) {

        Map<Character, String> map = new HashMap<>();
        map.put('2', "abc");
        map.put('3', "def");
        map.put('4', "ghi");
        map.put('5', "jkl");
        map.put('6', "mno");
        map.put('7', "pqrs");
        map.put('8', "tuv");
        map.put('9', "wxyz");
        List<String> result = new ArrayList<>();
        if (digits.length() == 0) {
            return result;
        }

        StringBuilder stringBuilder = new StringBuilder();
        deal(map, stringBuilder, 0, result, digits);
        return result;
    }

    public void deal(Map<Character, String> map, StringBuilder stringBuilder, int index, List<String> result, String digits) {
        if (digits.length() == index) {
            result.add(stringBuilder.toString());
        } else {
            char digit = digits.charAt(index);
            String words = map.get(digit);
            int length = words.length();
            for (int i = 0; i < length; i++) {
                stringBuilder.append(words.charAt(i));
                deal(map, stringBuilder, index + 1, result, digits);
                stringBuilder.deleteCharAt(index);
            }
        }

    }
}
